var dateTimePicker = require('../../utils/dateTimePicker.js');
import {request} from "../../request/index.js"



Page({
  data: {
    date: '2018-10-01',
    time: '12:00',
    dateTimeArray: null,
    dateTime: null,
    dateTimeArray1: null,
    dateTime1: null,
    dateTimeArray2: null,
    dateTime2: null,
    startYear: 2000,
    endYear: 2050,
    show: false,
    show2: false,
    show3: false,
    text: '请选择母线',
    text2: '请选择出线',
    text3: '请选择线路',
    one: [],
    num: '',
    dataTimeString: '',
    dataTimeString1: '',
    dataTimeString2: '',
    belong10kv: '',
    userName: '',
    lineList1: [],
    lineList2: [],
    lineList3: [],
    busBar: '',
    superior: '',
    },
  onLoad(){
    // 获取完整的年月日 时分秒，以及默认显示的数组
    var obj = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
    var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);

    
    this.setData({
      dateTime: obj.dateTime,
      dateTimeArray: obj.dateTimeArray,
      dateTimeArray1: obj1.dateTimeArray,
      dateTime1: obj1.dateTime,
      dateTimeArray2: obj2.dateTimeArray,
      dateTime2: obj2.dateTime
    });
  },
  onShow() {
    request({
      url: '/api/list/line/tree',
      data: {
      },
      header: {

      }
    })
    .then(res => {
      var primaryList = res.data.data
      var primary = []
      var onePrimary = []
      primaryList.forEach((item, i) => {
        primary.push(item)
      })
      this.setData({
        one: primary
      })
    })
  },
  changeDate(e){
    this.setData({ date:e.detail.value});
  },
  changeTime(e){
    this.setData({ time: e.detail.value });
  },
  changeDateTime(e){
    var arr = e.detail.value
    var year = '20' + (arr[0]>10?arr[0]: ('0' + arr[0]))
    var month = arr[1] + 1
    var data = arr[2] + 1
    var hour = arr[3]
    var min = arr[4]
    var second = arr[5]
    
    this.setData({ 
      dateTime: e.detail.value,
      dataTimeString: year + '-' + month + '-' + data + ' ' + hour + ':' + min + ':' + second,
    });
  },
  changeDateTime1(e) {
    var arr = e.detail.value
    var year = '20' + (arr[0]>10?arr[0]: ('0' + arr[0]))
    var month = arr[1] + 1
    var data = arr[2] + 1
    var hour = arr[3]
    var min = arr[4]
    var second = arr[5]
    this.setData({ 
      dateTime1: e.detail.value,
      dataTimeString1: year + '-' + month + '-' + data + ' ' + hour + ':' + min + ':' + second,
    })
  },
  changeDateTime2(e) {
    console.log(e)
    var arr = e.detail.value
    var year = '20' + (arr[0]>10?arr[0]: ('0' + arr[0]))
    var month = arr[1] + 1
    var data = arr[2] + 1
    var hour = arr[3]
    var min = arr[4]
    var second = arr[5]
    this.setData({ 
      dateTime2: e.detail.value,
      dataTimeString2: year + '-' + month + '-' + data + ' ' + hour + ':' + min + ':' + second,
    })
  },
  changeDateTimeColumn(e){
    var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({
      dateTimeArray: dateArr,
      dateTime: arr
    });
  },
  changeDateTimeColumn1(e) {
    var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({ 
      dateTimeArray1: dateArr,
      dateTime1: arr
    });
  },
  changeDateTimeColumn2(e) {
    var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;

    arr[e.detail.column] = e.detail.value;
    dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);

    this.setData({ 
      dateTimeArray2: dateArr,
      dateTime2: arr
    });
  },
  bindShowMsg() {
    this.setData({
      show: !this.data.show,
      text2: '请选择出线',
      text3: '请选择出线',
      lineList3: [],
      lineList2: []
    })

    request({
      url: "/api/list/busbar",
      data: {}
    })
    .then(res => {
      this.setData({
        lineList1: res.data.data
      })
    })

  },
  bindShowMsg2() {
    this.setData({
      show2: !this.data.show2,
      text3: '请选择出线',
      lineList3: []
    })
  },
  bindShowMsg3() {
    this.setData({
      show3: !this.data.show3
    })
  },
  selectItem(e) {
    var busBar = e.currentTarget.dataset.name
    this.setData({
      busBar
    })
    if (busBar.length == 0 || busBar == undefined) {
      return false;
    } else {
      request({
        url: "/api/list/superior/?busBar=" + busBar,
        data: {

        }
      })
      .then(res => {
        this.setData({
          lineList2: res.data.data
        })
      })
      if(busBar.length > 6) {
        busBar = busBar.substring(0,6) + "...";
        this.setData({
          text: busBar,
          show: false
        })
      } else {
        this.setData({
          text: busBar,
          show: false
        })
      }  
    } 
    
    
  },
  selectItem2(e) {
    console.log(e)
    var superior = e.currentTarget.dataset.name
    console.log(superior)
    this.setData({
      superior
    })
    if (superior.length == 0 || superior == undefined) {
      return false;
    } else {
      request({
        url: "/api/list/belong/?superior=" + superior,
        data: {

        }
      })
      .then(res => {
        console.log(res)
        var data = res.data.data
        
        this.setData({
          lineList3: data
        })
      })

      if(superior.length > 6) {
        superior = superior.substring(0,6) + "...";
        this.setData({
          text2: superior,
          show2: false
        })
      } else {
        this.setData({
          text2: superior,
          show2: false
        })
      }
    }
  },
  selectItem3(e) {
    var name = e.currentTarget.dataset.name
    this.setData({
      belong10kv: name
    })
    if (name.length == 0 || name == undefined) {
      return false;
    } else if(name.length > 6) {
      name = name.substring(0,6) + "...";
      this.setData({
        text3: name,
        show3: false
      })
    } else {
      this.setData({
        text3: name,
        show3: false
      })
    }
  },
  getName(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  issue() {
    request({
      url: '/api/entering',
      method: 'POST',
      data: {
        endTime: this.data.dataTimeString1,
        belong10kv: this.data.belong10kv,
        name: this.data.userName,
        pullTime: this.data.dataTimeString2,
        startTime: this.data.dataTimeString,
        stationAndBusbar: this.data.busBar,
        superior35kv: this.data.superior
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      }
    })
    .then(res => {
      console.log(res)
      if(res.data.code === 500) {
        wx.showToast({
          title: res.data.message,
          mask: true,
          icon: 'none'
        })
      } else {
        wx.navigateBack({
          url: '/pages/issue/issue',
        })
      }
    })
  }
})